import request, { type ListQuery, type ListResponse } from "@/utils/request";
import { type RegionalAgentItemType } from "../user/regionalAgent";
import { type UserProfileType } from "../user/profile";

const SUPPLIER_BASE_URL = "/api";

export const SupplierApi = {
  getSupplierList: (params: ListQuery) => {
    return request.get<any, ListResponse<SupplierItemType>>(
      `${SUPPLIER_BASE_URL}/internal/supplier/`,
      { params }
    );
  },
  getSupplierDetail: (id: number) => {
    return request.get(`${SUPPLIER_BASE_URL}/internal/supplier/${id}/`);
  },
  createSupplier: (data: Partial<SupplierForm>) => {
    return request.post(`${SUPPLIER_BASE_URL}/internal/supplier/`, data);
  },
  updateSupplier: (data: Partial<SupplierForm>) => {
    return request.put(`${SUPPLIER_BASE_URL}/internal/supplier/${data.id}/`, data);
  },
  deleteSupplier: (id: number) => {
    return request.delete(`${SUPPLIER_BASE_URL}/internal/supplier/${id}/`);
  },
};

export type SupplierListQuery = {} & ListQuery;

export type SupplierItemType = {
  id: number;
  user: UserProfileType;
  agent: RegionalAgentItemType;
  name: string;
  contact_name: string;
  contact_phone: string;
  contact_email: string;
  is_active: boolean;
};

export type SupplierForm = {
  id?: number;
  user: number | string;
  agent: number | string;
  name: string;
  contact_name: string;
  contact_phone: string;
  contact_email: string;
  is_active: boolean;
};

export default SupplierApi;
